package com.dbgs.insurance.insuredperson.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.dbgs.insurance.insuredperson.models.GpsModel;
import com.hss.base.util.LogUtils;


public class MyLocationService extends Service {
	// 定位相关
	LocationClient mLocClient;
	public MyLocationListenner myListener = new MyLocationListenner();
	boolean isRequest = false;// 是否手动触发请求定位
	boolean isFirstLoc = true;// 是否首次定位

//	String cityName, cityId, cityNameDB, cityCode;

	@Override
	public IBinder onBind(Intent intent) {
		return mLocationBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();

	/*	LocationClientOption option = new LocationClientOption();
		option.setLocationMode(LocationMode.Hight_Accuracy);
		//可选，默认高精度，设置定位模式，高精度，低功耗，仅设备

		option.setCoorType("bd09ll");
		//可选，默认gcj02，设置返回的定位结果坐标系

		int span=1000;
		option.setScanSpan(span);
		//可选，默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms才是有效的

		option.setIsNeedAddress(true);
		//可选，设置是否需要地址信息，默认不需要

		option.setOpenGps(true);
		//可选，默认false,设置是否使用gps

		option.setLocationNotify(true);
		//可选，默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果

		option.setIsNeedLocationDescribe(true);
		//可选，默认false，设置是否需要位置语义化结果，可以在BDLocation.getLocationDescribe里得到，结果类似于“在北京天安门附近”

		option.setIsNeedLocationPoiList(true);
		//可选，默认false，设置是否需要POI结果，可以在BDLocation.getPoiList里得到

		option.setIgnoreKillProcess(false);
		//可选，默认true，定位SDK内部是一个SERVICE，并放到了独立进程，设置是否在stop的时候杀死这个进程，默认不杀死

		option.SetIgnoreCacheException(false);
		//可选，默认false，设置是否收集CRASH信息，默认收集

		option.setEnableSimulateGps(false);
		//可选，默认false，设置是否需要过滤GPS仿真结果，默认需要
*/
		// 定位初始化
		mLocClient = new LocationClient(this);
		mLocClient.registerLocationListener(myListener);
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(false);// 打开gps
		option.setCoorType("bd09ll"); // 设置坐标类型
		option.setScanSpan(30000 );//* 30
		option.setAddrType("all");
		mLocClient.setLocOption(option);
		mLocClient.start();
		mLocClient.requestLocation();
		LogUtils.e("____________________MyLocationService onCreate ___________________________________");
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		LogUtils.e("____________________MyLocationService onDestroy ___________________________________");

		if (mLocClient != null) {
			mLocClient.stop();
			mLocClient.unRegisterLocationListener(myListener);
		}
	}

	/**
	 * 定位SDK监听函数
	 */
	public class MyLocationListenner implements BDLocationListener {

		@Override
		public void onReceiveLocation(BDLocation location) {
			if (location == null){
//				LogUtils.e(" location == null");
				return;
			}

			// 是手动触发请求或首次定位时，移动到定位点
			if (isRequest || isFirstLoc) {
				// 移动地图到定位点
				Log.d("LocationOverlay", "receive location, animate to it");
				// ToastUtil.showToast(SelectCityActivity.this,
				// "receive location, animate to it", false);
				isRequest = false;
				// String keyword = location.getCity();
				// int end = keyword.indexOf("市");
				// if (end > 0) {
				// keyword = keyword.substring(0, end);
				// keyword = "%" + keyword + "%";
				// }
				// AddressDB db = new AddressDB(this);
				// gpsAddress = db.querySimilarByName(keyword, 2);
				// if (gpsAddress != null){
				// selectcities_default_cityname_tv.setText(gpsAddress.name);
				// }
				// db.destory();
				//
				// if (status == 0){
				// curCity = gpsAddress;
				// }

			}
			// 首次定位完成
			isFirstLoc = false;
//			String keyword = location.getCity();
//			if (keyword != null) {
//				if (cityName == null || !cityName.equals(keyword)) {
//					cityName = keyword;
//					int end = keyword.indexOf("市");
//					if (end > 0) {
//						keyword = keyword.substring(0, end);
//						keyword = "%" + keyword + "%";
//					}
////					AddressDB db = new AddressDB(MyLocationService.this);
////					AddressBean gpsAddress = db.querySimilarByName(keyword, 2);
////					db.destory();
////					cityId = gpsAddress.id;
////					cityNameDB = gpsAddress.name;
////					cityCode = gpsAddress.code;
////					gpsAddress =  null;
//				}
//			}
//			if (application == null){
//				application = (MyApplication)getApplication();
//			}
//			if (application != null){
//				application.cityId = cityId;
//				application.cityName = cityNameDB;
//				application.cityCode = cityCode ;
//				application.lat = location.getLatitude();
//				application.lon = location.getLongitude();
//			}
			
//			sendMsg(location.getLatitude(), location.getLongitude());


			GpsModel.getInstance().setLocation(location);
		}

		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
		}
	}

	
	
	// 发送广播信息
	private void sendMsg(double lat, double lon) {
//		LogUtil.e("lat = "+lat +"	lon = "+lon);
		// 指定广播目标的 action （注：指定了此 action 的 receiver 会接收此广播）
//		Intent intent = new Intent(ConstStr.LOCATION_ACTION);
//		// 需要传递的参数
//		Bundle bundle = new Bundle();
//		if (cityName != null){
//			bundle.putString(ConstStr.KEY_CITYNAME, cityNameDB);
//		}
//		if (cityId != null){
//			bundle.putString(ConstStr.KEY_CITYID, cityId);
//		}
//		if (cityCode != null){
//			bundle.putString(ConstStr.KEY_CITYCODE, cityCode);
//		}
//		bundle.putDouble(ConstStr.KEY_LAT,lat);
//		bundle.putDouble(ConstStr.KEY_LON,lon);
//		intent.putExtras(bundle);// 将参数 绑定到intent中
//		// 发送广播
//		sendBroadcast(intent);

//		LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(MyLocationService.this);

//		localBroadcastManager.sendBroadcast(intent);
	}



	//此方法是为了可以在Acitity中获得服务的实例
	public	class LocationBinder extends Binder {
		public MyLocationService getService() {
			return MyLocationService.this;
		}
	}

	LocationBinder mLocationBinder = new LocationBinder();
}
